<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="" prop="plate">
        <el-input
          v-model="queryParams.plate"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="driverName">
        <el-input
          v-model="queryParams.driverName"
          placeholder="请输入司机姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入司机电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="" prop="vehicleTypeCode">
        <el-select v-model="queryParams.vehicleTypeCode" placeholder="请选择轴型" clearable filterable>
          <el-option
            v-for="dict in dict.type.vehicle_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="truckType">
        <el-select v-model="queryParams.truckType" placeholder="请选择车型" clearable filterable>
          <el-option
            v-for="dict in dict.type.truck_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:truckDriver:add']"
        >新增</el-button>
      </el-form-item>
    </el-form>
    <!--
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:truckDriver:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:truckDriver:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:truckDriver:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:truckDriver:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
     -->

    <el-table v-loading="loading" :data="truckDriverList" @selection-change="handleSelectionChange" border>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="序号" align="center" type="index" fixed/>
      <el-table-column label="车牌号" align="center" prop="plate" fixed width="150"/>
      <el-table-column label="挂车号" align="center" prop="plateGua" width="150"/>
      <el-table-column label="车牌颜色" align="center" prop="plateColor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.plate_color" :value="scope.row.plateColor"/>
        </template>
      </el-table-column>
      <el-table-column label="轴数" align="center" prop="axisCount" />
      <el-table-column label="轴型" align="center" prop="vehicleTypeCode">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.vehicle_type" :value="scope.row.vehicleTypeCode"/>
        </template>
      </el-table-column>
      <el-table-column label="车型" align="center" prop="truckType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.truck_type" :value="scope.row.truckType"/>
        </template>
      </el-table-column>
      <el-table-column label="车重/t" align="center" prop="weight" />
      <el-table-column label="限重/t" align="center" prop="limitWeight" />

      <el-table-column label="车辆颜色" align="center" prop="truckColor">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.plate_color" :value="scope.row.truckColor"/>
        </template>
      </el-table-column>
      <el-table-column label="车辆厂牌" align="center" prop="truckBrand" />
      <!-- <el-table-column label="车辆照片" align="center" prop="truckPic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.truckPic" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <el-table-column label="司机姓名" align="center" prop="driverName" />
      <el-table-column label="司机身份证" align="center" prop="driverCard" width="180" />
      <el-table-column label="司机电话" align="center" prop="driverPhone" width="150"/>
      <!-- <el-table-column label="从业资格证" align="center" prop="driverCyz" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.driverCyz" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="运输证" align="center" prop="driverYsz" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.driverYsz" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <el-table-column label="操作" width="150" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:truckDriver:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:truckDriver:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改货车信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" :width="viewPortWidth > 1000 ?1000 + 'px': viewPortWidth - 20 + 'px'" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="车牌号" prop="plate">
          <el-input v-model="form.plate" placeholder="请输入车牌号" />
        </el-form-item>
        <el-form-item label="挂车号" prop="plateGua">
          <el-input v-model="form.plateGua" placeholder="请输入挂车号" />
        </el-form-item>
        <el-form-item label="车牌颜色" prop="plateColor">
          <el-select v-model="form.plateColor" placeholder="请选择车牌颜色" filterable>
            <el-option
              v-for="dict in dict.type.plate_color"
              :key="dict.value"
              :label="dict.label" :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="轴数" prop="axisCount">
          <el-input v-model="form.axisCount" placeholder="请输入轴数" />
        </el-form-item>
        <el-form-item label="轴型" prop="vehicleTypeCode">
          <el-select v-model="form.vehicleTypeCode" placeholder="请选择轴型" filterable>
            <el-option
              v-for="dict in dict.type.vehicle_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车型" prop="truckType">
          <el-select v-model="form.truckType" placeholder="请选择车型" filterable>
            <el-option
              v-for="dict in dict.type.truck_type"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车重/t" prop="weight">
          <el-input v-model="form.weight" placeholder="请输入车重/t" />
        </el-form-item>
        <el-form-item label="限重/t" prop="limitWeight">
          <el-input v-model="form.limitWeight" placeholder="请输入限重/t" />
        </el-form-item>
        <el-form-item label="速度/km/h" prop="speed">
          <el-input v-model="form.speed" placeholder="请输入速度/km/h" />
        </el-form-item>
        <el-form-item label="长" prop="length">
          <el-input v-model="form.length" placeholder="请输入长" />
        </el-form-item>
        <el-form-item label="宽" prop="width">
          <el-input v-model="form.width" placeholder="请输入宽" />
        </el-form-item>
        <el-form-item label="高" prop="height">
          <el-input v-model="form.height" placeholder="请输入高" />
        </el-form-item>
        <el-form-item label="车辆颜色" prop="truckColor">
          <el-select v-model="form.truckColor" placeholder="请选择车辆颜色" filterable>
            <el-option
              v-for="dict in dict.type.plate_color"
              :key="dict.value"
              :label="dict.label"
:value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="车辆厂牌" prop="truckBrand">
          <el-input v-model="form.truckBrand" placeholder="请输入车辆厂牌" />
        </el-form-item>
        <el-form-item label="车辆照片" prop="truckPic">
          <image-upload v-model="form.truckPic"/>
        </el-form-item>
        <el-form-item label="司机姓名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机姓名" />
        </el-form-item>
        <el-form-item label="身份证" prop="driverCard">
          <el-input v-model="form.driverCard" placeholder="请输入身份证" />
        </el-form-item>
        <el-form-item label="司机电话" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机电话" />
        </el-form-item>
        <el-form-item label="从业资格证" prop="driverCyz">
          <image-upload v-model="form.driverCyz"/>
        </el-form-item>
        <el-form-item label="运输证" prop="driverYsz">
          <image-upload v-model="form.driverYsz"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listTruckDriver, getTruckDriver, delTruckDriver, addTruckDriver, updateTruckDriver } from "@/api/business/truckDriver";

export default {
  name: "TruckDriver",
  dicts: ['plate_color', 'truck_type', 'vehicle_type'],
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 货车信息表格数据
      truckDriverList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        plate: null,
        plateGua: null,
        plateColor: null,
        axisCount: null,
        vehicleTypeCode: null,
        truckType: null,
        weight: null,
        limitWeight: null,
        speed: null,
        length: null,
        width: null,
        height: null,
        truckColor: null,
        truckBrand: null,
        truckPic: null,
        driverName: null,
        driverCard: null,
        driverPhone: null,
        driverCyz: null,
        driverYsz: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        plate: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        driverName: [
          { required: true, message: "司机姓名不能为空", trigger: "blur" }
        ],
        driverPhone: [
          { required: true, message: "司机电话不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  computed:{
    viewPortWidth(){
      return window.innerWidth || document.documentElement.clientWidth;
    }
  },
  methods: {
    /** 查询货车信息列表 */
    getList() {
      this.loading = true;
      listTruckDriver(this.queryParams).then(response => {
        this.truckDriverList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        plate: null,
        plateGua: null,
        plateColor: null,
        axisCount: null,
        vehicleTypeCode: null,
        truckType: null,
        weight: null,
        limitWeight: null,
        speed: null,
        length: null,
        width: null,
        height: null,
        truckColor: null,
        truckBrand: null,
        truckPic: null,
        driverName: null,
        driverCard: null,
        driverPhone: null,
        driverCyz: null,
        driverYsz: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加货车信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTruckDriver(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改货车信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateTruckDriver(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTruckDriver(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除货车信息编号为"' + ids + '"的数据项？').then(function() {
        return delTruckDriver(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/truckDriver/export', {
        ...this.queryParams
      }, `truckDriver_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
